package com.example.project3.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.project3.LoginActivity;
import com.example.project3.R;
import com.example.project3.InterestSelectActivity;
import com.example.project3.BookManagementActivity;

public class MineFragment extends Fragment {
    private TextView tvEmail;
    private TextView tvInterest;
    private Button btnSelectInterest;
    private Button btnMyBooks;
    private Button btnLogout;
    private String currentEmail;
    private String currentInterest;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);

        tvEmail = view.findViewById(R.id.tv_email);
        tvInterest = view.findViewById(R.id.tv_interest);
        btnSelectInterest = view.findViewById(R.id.btn_select_interest);
        btnMyBooks = view.findViewById(R.id.btn_my_books);
        btnLogout = view.findViewById(R.id.btn_logout);

        // 恢复保存的状态
        if (savedInstanceState != null) {
            currentEmail = savedInstanceState.getString("email");
            currentInterest = savedInstanceState.getString("interest");
        } else {
            // 从SharedPreferences获取用户信息
            SharedPreferences sp = requireActivity().getSharedPreferences("user_info", 0);
            currentEmail = sp.getString("email", "");
            currentInterest = sp.getString("interest", getString(R.string.no_interest));
        }

        // 更新UI显示
        tvEmail.setText("邮箱：" + currentEmail);
        tvInterest.setText("当前兴趣：" + currentInterest);

        // 选择兴趣按钮点击事件
        btnSelectInterest.setOnClickListener(v -> {
            Intent intent = new Intent(requireActivity(), InterestSelectActivity.class);
            startActivityForResult(intent, 100); // 100是请求码
        });

        // 我的书籍按钮点击事件
        btnMyBooks.setOnClickListener(v -> {
            Intent intent = new Intent(requireActivity(), BookManagementActivity.class);
            startActivity(intent);
        });

        // 退出登录按钮点击事件
        btnLogout.setOnClickListener(v -> {
            // 清除登录信息
            SharedPreferences sp = requireActivity().getSharedPreferences("user_info", 0);
            sp.edit().clear().apply();

            // 跳回登录页面
            Intent intent = new Intent(requireActivity(), LoginActivity.class);
            startActivity(intent);
            requireActivity().finish(); // 关闭主页面
        });

        return view;
    }

    // 接收兴趣选择页面返回的结果
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == requireActivity().RESULT_OK && data != null) {
            // 获取选择的兴趣
            String newInterest = data.getStringExtra("interest");
            if (newInterest != null) {
                currentInterest = newInterest;
                // 更新UI
                tvInterest.setText("当前兴趣：" + currentInterest);

                // 保存到SharedPreferences
                SharedPreferences sp = requireActivity().getSharedPreferences("user_info", 0);
                sp.edit().putString("interest", currentInterest).apply();

                // 更新首页Fragment的兴趣显示
                HomeFragment homeFragment = (HomeFragment) requireActivity()
                        .getSupportFragmentManager()
                        .findFragmentById(R.id.fragment_container);
                if (homeFragment != null) {
                    homeFragment.setInterest(currentInterest);
                }
            }
        }
    }

    // 保存Fragment状态
    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("email", currentEmail);
        outState.putString("interest", currentInterest);
    }
}